外观
Rsyslog 服务配置与部署详解
1. Rsyslog 核心架构
1.1 消息流概述
Rsyslog 消息处理流程:
1.2 处理原则
Rsyslog 的核心处理原则包括:
- 消息导向:所有处理都围绕日志消息进行
- 规则驱动:通过规则集控制消息的处理流程
- 模块化扩展:功能通过模块动态加载
- 配置优先:配置决定处理行为和输出格式
1.2 七大核心模块概述
Rsyslog 的配置基于七大核心模块,构成了完整的日志处理流水线:
| 模块 | 功能描述 | 配置语法 |
|---|---|---|
| module | 加载输入/输出/解析器模块 | module(load="模块名") |
| input | 定义日志输入源(UDP) | input(type="imudp" 参数...) |
| rules | 定义消息处理规则和过滤条件 | if 条件 then { 动作 } |
| action | 定义消息输出动作(文件) | action(type="omfile" 参数...) |
| global | 设置全局运行参数 | global(参数="值") |
| template | 定义日志输出格式模板 | template(name="模板名" type="类型" string="格式") |
| include | 包含外部配置文件 | include(file="路径") |
配置顺序要求
七大模块必须按照特定顺序配置:module → global → include → input → rules → action → template。错误的顺序会导致配置失败。
2. 模块加载 (Module)
2.1 模块加载语法
模块加载是 Rsyslog 配置的第一步,用于启用所需的输入、输出和解析功能。
2.1.1 基本语法
bash
# 加载输入模块
module(load="imudp") # UDP 输入模块
module(load="imrelp") # RELP 输入模块
# 加载输出模块
module(load="omfile") # 文件输出模块
# 加载解析器模块
module(load="pmnormalize") # 通用解析器
module(load="mmnormalize") # 消息修改模块1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
2.1.2 模块参数配置
某些模块支持加载时配置参数:
bash
# 加载模块时配置参数
module(load="imudp" threads="4") # UDP模块使用4个线程
# 标准配置语法
module(load="imudp")
input(type="imudp" port="514")1
2
3
4
5
6
2
3
4
5
6
2.2 常用模块介绍
2.2.1 输入模块
| 模块名 | 功能 | 使用场景 |
|---|---|---|
| imudp | UDP Syslog 输入 | 高性能场景,允许丢包 |
| imuxsock | Unix 域套接字输入 | 本地系统日志 |
| imklog | 内核日志输入 | Linux 内核消息 |
2.2.2 输出模块
| 模块名 | 功能 | 使用场景 |
|---|---|---|
| omfile | 文件输出 | 本地日志存储 |
| omfwd | 网络转发 | 远程日志服务器 |
2.2.3 解析器和处理模块
| 模块名 | 功能 | 使用场景 |
|---|---|---|
| pmnormalize | 日志规范化 | 结构化非结构化日志 |
| mmnormalize | 消息修改 | 日志内容转换 |
| mmjsonparse | JSON 解析 | 处理 JSON 格式日志 |
| mmfields | 字段提取 | 从日志中提取字段 |
模块选择建议
- 开发测试环境:从 imudp 和 omfile 开始
- 生产环境:配置适当的速率限制和安全措施
3. 输入配置 (Input)
3.1 UDP 输入概述
UDP (User Datagram Protocol) 是 Rsyslog 最常用的网络输入协议,适合高性能日志收集场景。UDP 具有低延迟、高性能的特点,但不保证消息可靠性,适用于允许少量丢包的监控和审计日志。
3.2 UDP 输入配置
3.2.1 基础 UDP 配置
bash
# 加载UDP模块
module(load="imudp")
# 基础UDP输入配置
input(type="imudp" port="514")
# 完整配置示例
input(type="imudp"
port="514"
address="0.0.0.0"
ruleset="udp_logs")1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
3.2.2 高性能 UDP 配置
bash
# 高性能UDP配置
module(load="imudp" threads="4")
input(type="imudp"
port="514"
address="0.0.0.0"
RateLimit.interval="30"
RateLimit.burst="20000"
rcvbufSize="1048576"
ruleset="high_perf_udp")1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
线程配置
threads 参数是 module 参数,不是 input 参数。需要在 module 加载时指定:
bash
module(load="imudp" threads="4") # 设置为4个线程1
3.2.3 UDP 配置参数详解
基于官方 imudp 模块文档,最常用的配置参数如下:
| 参数 | 默认值 | 说明 |
|---|---|---|
| port | 514 | 监听端口 |
| address | 127.0.0.1 | 指定 Rsyslog 服务绑定到哪个本地网络接口监听 UDP 包(多网卡情况下需要指定) |
| ruleset | 默认规则集 | 指定使用的规则集 |
| RateLimit.interval | 0 | 速率限制间隔(秒),0 表示禁用 |
| RateLimit.burst | 10000 | 每个间隔允许的最大消息数 |
| inputname | imudp | 输入源名称,用于统计和标识 |
其他参数
官方文档还包含其他高级参数如 rcvbufSize、inputname.appendPort、DefaultTZ 等,在大多数场景下使用默认值即可。
4. 规则配置 (Rules)
4.1 规则语法结构
规则是 Rsyslog 的核心,用于控制如何处理和路由日志消息。
4.1.1 基本规则语法
bash
# 简单条件规则
if 条件表达式 then {
动作1
动作2
}
# 多条件规则
if 条件1 and 条件2 then {
动作
}
# 复杂规则示例
if $programname == 'sshd' and $severity <= 'info' then {
action(type="omfile" file="/var/log/sshd.log")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
4.2 过滤条件
4.2.1 属性过滤
| 属性 | 说明 | 示例 |
|---|---|---|
| $programname | 程序名称 | $programname == 'apache' |
| $hostname | 主机名 | $hostname contains 'web' |
| $severity | 严重级别 | $severity <= 'info' |
| $facility | 设施类型 | $facility == 'mail' |
| $msg | 消息内容 | $msg contains 'error' |
| $fromhost-ip | 源 IP 地址 | $fromhost-ip == '192.168.1.100' |
4.2.2 表达式过滤
bash
# 字符串匹配
if $msg contains 'ERROR' then {
action(type="omfile" file="/var/log/errors.log")
}
# 正则表达式匹配
if re_match($msg, 'error|fail|exception') then {
action(type="omfile" file="/var/log/app-errors.log")
}
# 数值比较
if $severity <= 4 then { # 4=warning, 3=error, etc.
action(type="omfile" file="/var/log/warnings.log")
}
# 复合条件
if $programname == 'nginx' and ($msg contains 'error' or $msg contains 'fail') then {
action(type="omfile" file="/var/log/nginx-errors.log")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4.3 规则集 (Rulesets)
规则集允许将不同来源的日志应用不同的处理规则。
4.3.1 规则集定义
bash
# 定义规则集
ruleset(name="app_logs") {
# 应用日志处理规则
if $programname == 'apache' then {
action(type="omfile" file="/var/log/apache.log")
}
if $programname == 'nginx' then {
action(type="omfile" file="/var/log/nginx.log")
}
# 默认动作
action(type="omfile" file="/var/log/app-default.log")
}
ruleset(name="system_logs") {
# 系统日志处理规则
if $facility == 'kern' then {
action(type="omfile" file="/var/log/kern.log")
}
action(type="omfile" file="/var/log/syslog")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4.3.2 规则集应用
bash
# 将不同输入绑定到不同规则集
input(type="imudp" port="5140" inputname="app_udp" ruleset="app_logs")
input(type="imudp" port="5141" inputname="system_udp" ruleset="system_logs")1
2
3
2
3
4.4 规则配置示例
4.4.1 完整的规则配置
bash
# 加载必要模块
module(load="imudp")
module(load="omfile")
# 定义模板
template(name="DetailedFormat" type="string"
string="%TIMESTAMP:::date-rfc3339% %HOSTNAME% %syslogtag% %msg%\n")
# 定义规则集
ruleset(name="production_logs") {
# SSH日志
if $programname == 'sshd' then {
action(type="omfile"
file="/var/log/sshd.log"
template="DetailedFormat")
}
# Web服务器日志
if $programname == 'apache' or $programname == 'nginx' then {
action(type="omfile"
file="/var/log/web.log"
template="DetailedFormat")
}
# 错误日志
if $msg contains 'error' or $msg contains 'ERROR' then {
action(type="omfile"
file="/var/log/errors.log"
template="DetailedFormat")
}
# 默认处理
action(type="omfile"
file="/var/log/messages"
template="DetailedFormat")
}
# 应用规则集
input(type="imudp" port="514" ruleset="production_logs")1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
5. 动作配置 (Action)
5.1 文件输出动作 (omfile)
文件输出是 Rsyslog 最常用的动作,用于将日志写入本地文件系统。
5.1.1 基础文件输出
bash
# 基础文件输出
action(type="omfile" file="/var/log/app.log")
# 带模板的文件输出
action(type="omfile"
file="/var/log/app.log"
template="MyTemplate")1
2
3
4
5
6
7
2
3
4
5
6
7
5.1.2 高级文件输出配置
bash
# 完整的文件输出配置
action(type="omfile"
file="/var/log/app.log"
template="DetailedFormat"
fileOwner="syslog"
fileGroup="adm"
fileCreateMode="0644"
dirCreateMode="0755")1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
5.1.3 日志轮询配置
日志轮询是防止日志文件无限增长的重要机制。
5.1.3.1 使用 logrotate 进行外部轮询
bash
# /etc/logrotate.d/rsyslog 配置
/var/log/app.log {
daily
rotate 30
compress
missingok
notifempty
create 644 syslog adm
postrotate
/bin/kill -HUP `cat /var/run/rsyslogd.pid`
endscript
}1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
5.1.3.2 Rsyslog 内置轮询配置
bash
# 使用 omfile 的内置轮询功能
action(type="omfile"
file="/var/log/app.log"
template="MyTemplate"
rotation.size="100m" # 文件大小轮询
rotation.time="daily" # 时间轮询
rotation.maxFiles="30" # 保留文件数量
rotation.compress="on") # 压缩旧文件1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
5.1.3.3 动态文件名轮询
bash
# 基于时间的动态文件名
template(name="DailyLog" type="string"
string="/var/log/app/app-%$YEAR%-%$MONTH%-%$DAY%.log")
action(type="omfile"
dynaFile="DailyLog"
template="MyTemplate")
# 基于主机名的动态文件名
template(name="HostLog" type="string"
string="/var/log/hosts/%hostname%.log")
action(type="omfile"
dynaFile="HostLog"
template="MyTemplate")1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
日志轮询最佳实践
- 定期清理:设置合理的保留时间,避免磁盘空间不足
- 压缩存储:对历史日志进行压缩,节省存储空间
- 权限控制:确保日志文件有正确的访问权限
- 监控磁盘:监控日志目录的磁盘使用率
5.1.4 文件输出参数详解
基于官方 omfile 模块文档,主要配置参数如下:
| 参数 | 默认值 | 说明 |
|---|---|---|
| file | - | 输出文件路径 |
| template | RSYSLOG_Default | 使用的模板 |
| asyncWriting | off | 启用异步写入,提高性能 |
| flushInterval | 1 | 缓冲刷新间隔(秒) |
| ioBufferSize | 4k | I/O 缓冲区大小 |
| veryRobustZip | off | 启用压缩模式 |
| zipLevel | 6 | 压缩级别(0-9) |
| rotation.sizeLimit | - | 文件大小轮询限制 |
| rotation.timeLimit | - | 时间轮询限制 |
| rotation.maxFiles | - | 最大保留文件数 |
| fileOwner | - | 文件所有者 |
| fileGroup | - | 文件组 |
| fileCreateMode | 0644 | 文件创建权限 |
| dirCreateMode | 0755 | 目录创建权限 |
5.2 文件输出最佳实践
5.2.1 高性能文件输出
bash
# 高性能文件输出配置
action(type="omfile"
file="/var/log/high-volume.log"
template="SimpleFormat"
asyncWriting="on"
ioBufferSize="65536"
flushInterval="10")1
2
3
4
5
6
7
2
3
4
5
6
7
5.2.2 带轮询的文件输出
bash
# 自动轮询的文件输出
action(type="omfile"
file="/var/log/app.log"
template="MyTemplate"
rotation.sizeLimit="100m"
rotation.timeLimit="daily"
rotation.maxFiles="30"
fileCreateMode="0644")1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
文件输出配置技巧
- 异步写入:启用 asyncWriting 提高性能
- 缓冲优化:调整 ioBufferSize 优化 I/O 性能
- 定期轮询:配置大小和时间轮询,防止文件过大
- 权限控制:设置适当的文件和目录权限
6. 全局参数 (Global)
6.1 全局参数语法
全局参数影响 Rsyslog 的整体行为,必须在模块加载之后、其他配置之前设置。
bash
# 全局参数配置语法
global(parameter="value")1
2
2
6.2 核心全局参数
6.2.1 工作目录和权限参数
bash
# 工作目录设置
global(workDirectory="/var/lib/rsyslog")
# 文件权限设置
global(fileCreateMode="0644")
global(dirCreateMode="0755")
global(fileOwner="syslog")
global(fileGroup="adm")
global(umask="0022")1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
6.2.2 性能和资源参数
bash
# 性能调优参数
global(maxMessageSize="64k") # 最大消息大小
global(dropMsgsWithMaliciousDNSReply="on") # 丢弃恶意DNS响应
global(optimizeForUniprocessor="off") # 多核优化
# 资源限制
global(processInternalMessages="on") # 处理内部消息
global(preserveFQDN="on") # 保留完整域名1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
6.2.3 安全参数
bash
# 安全相关参数
global(security.abortOnIDResolutionFail="on") # ID解析失败时中止
global(security.enableCFS="on") # 启用控制流安全1
2
3
2
3
6.3 常用全局配置示例
6.3.1 生产环境全局配置
bash
# 生产环境推荐全局配置
global(workDirectory="/var/lib/rsyslog")
global(maxMessageSize="64k")
global(fileCreateMode="0644")
global(dirCreateMode="0755")
global(fileOwner="syslog")
global(fileGroup="adm")
global(umask="0022")
global(dropMsgsWithMaliciousDNSReply="on")
global(preserveFQDN="on")
global(processInternalMessages="on")1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
6.3.2 高性能环境全局配置
bash
# 高性能环境配置
global(workDirectory="/var/lib/rsyslog")
global(maxMessageSize="128k")
global(optimizeForUniprocessor="off")
global(fileCreateMode="0644")
global(dirCreateMode="0755")
global(dropMsgsWithMaliciousDNSReply="on")1
2
3
4
5
6
7
2
3
4
5
6
7
7. 模板定义 (Template)
7.1 模板语法
模板定义日志的输出格式,是 Rsyslog 配置的重要组成部分。
7.1.1 字符串模板
bash
# 基本字符串模板
template(name="MyTemplate" type="string"
string="%TIMESTAMP% %HOSTNAME% %syslogtag% %msg%\n")
# 高级字符串模板
template(name="DetailedTemplate" type="string"
string="%TIMESTAMP:::date-rfc3339% %HOSTNAME% %PROGRAMNAME%[%PROCID%]: %msg%\n")1
2
3
4
5
6
7
2
3
4
5
6
7
7.2 常用属性变量
| 变量 | 说明 | 示例输出 |
|---|---|---|
| %TIMESTAMP% | 时间戳 | Jan 1 12:00:00 |
| %HOSTNAME% | 主机名 | web-server-01 |
| %PROGRAMNAME% | 程序名 | nginx |
| %PROCID% | 进程 ID | 12345 |
| %MSG% | 消息内容 | Connection established |
| %SYSLOGTAG% | 系统标签 | nginx[12345]: |
7.3 模板配置示例
7.3.1 文件输出模板
bash
# 传统格式模板
template(name="TraditionalFormat" type="string"
string="%TIMESTAMP% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg%\n")
# RFC5424格式模板
template(name="RFC5424Format" type="string"
string="%TIMESTAMP:::date-rfc3339% %HOSTNAME% %syslogtag% %msg%\n")1
2
3
4
5
6
7
2
3
4
5
6
7
7.3.2 动态文件名模板
bash
# 按日期分文件
template(name="DailyLog" type="string"
string="/var/log/app/app-%$YEAR%-%$MONTH%-%$DAY%.log")
# 按主机分文件
template(name="HostLog" type="string"
string="/var/log/hosts/%hostname%.log")
# 使用动态模板
action(type="omfile" dynaFile="DailyLog" template="TraditionalFormat")1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
8. 包含配置 (Include)
8.1 包含配置语法
包含配置允许将配置文件分割成多个文件,便于管理。
bash
# 包含单个文件
include(file="/etc/rsyslog.d/10-app.conf")
# 包含目录下所有文件
include(file="/etc/rsyslog.d/*.conf")1
2
3
4
5
2
3
4
5
8.2 配置分割最佳实践
8.2.1 配置文件结构
/etc/rsyslog.conf # 主配置文件
/etc/rsyslog.d/
├── 00-global.conf # 全局参数
├── 10-inputs.conf # 输入配置
├── 20-templates.conf # 模板定义
├── 30-rules.conf # 规则配置
└── 40-outputs.conf # 输出配置1
2
3
4
5
6
7
2
3
4
5
6
7
8.2.2 示例配置分割
00-global.conf:
bash
# 全局参数配置
global(workDirectory="/var/lib/rsyslog")
global(maxMessageSize="64k")
global(fileCreateMode="0644")1
2
3
4
2
3
4
10-inputs.conf:
bash
# 输入配置
module(load="imudp")
input(type="imudp" port="514")1
2
3
2
3
20-templates.conf:
bash
# 模板定义
template(name="StandardFormat" type="string"
string="%TIMESTAMP% %HOSTNAME% %syslogtag% %msg%\n")1
2
3
2
3
30-rules.conf:
bash
# 规则配置
if $programname == 'sshd' then {
action(type="omfile" file="/var/log/sshd.log")
}1
2
3
4
2
3
4
主配置文件 /etc/rsyslog.conf:
bash
# 包含所有子配置文件
include(file="/etc/rsyslog.d/*.conf")1
2
2
配置管理建议
- 模块化管理:将不同功能配置分离到不同文件
- 版本控制:将配置纳入版本控制系统
- 备份策略:定期备份配置文件
- 测试环境:在测试环境验证配置变更
9. 配置指南
9.1 完整配置示例
bash
# 全局配置
global(workDirectory="/var/lib/rsyslog")
global(maxMessageSize="64k")
global(fileCreateMode="0644")
global(dirCreateMode="0755")
# 模块加载
module(load="imudp")
# UDP 输入配置
input(type="imudp"
port="514"
threads="4"
RateLimit.interval="30"
RateLimit.burst="10000")
# 模板定义
template(name="StandardFormat" type="string"
string="%timestamp% %hostname% %syslogtag% %msg%\n")
# 文件输出配置
if $programname == 'sshd' then {
action(type="omfile"
file="/var/log/sshd.log"
template="StandardFormat"
rotation.sizeLimit="100m"
rotation.maxFiles="30")
}
action(type="omfile"
file="/var/log/messages"
template="StandardFormat")1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
9.2 配置验证和测试
9.2.1 语法验证
bash
# 验证配置文件语法
rsyslogd -N1 -f /etc/rsyslog.conf
# 查看详细错误信息
rsyslogd -dn -f /etc/rsyslog.conf1
2
3
4
5
2
3
4
5
9.2.2 服务测试
bash
# 重载配置
systemctl reload rsyslog
# 查看服务状态
systemctl status rsyslog
# 查看日志
journalctl -u rsyslog -f1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
配置注意事项
- 备份配置:修改前备份原有配置文件
- 逐步测试:分阶段进行配置修改和测试
- 监控日志:配置变更后监控系统日志,确保无错误
- 权限检查:确保 Rsyslog 有适当的文件访问权限
10. 部署和运维
10.1 安装部署
10.1.1 Ubuntu/Debian 安装
bash
# 更新包索引
sudo apt update
# 安装rsyslog
sudo apt install rsyslog
# 启动服务
sudo systemctl start rsyslog
sudo systemctl enable rsyslog1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
10.1.2 CentOS/RHEL 安装
bash
# 安装rsyslog
sudo yum install rsyslog
# 或者使用dnf
sudo dnf install rsyslog
# 启动服务
sudo systemctl start rsyslog
sudo systemctl enable rsyslog1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
10.2 性能监控
10.2.1 关键指标监控
bash
# 查看rsyslog进程状态
ps aux | grep rsyslog
# 查看队列状态(需要配置imstats模块)
module(load="imstats")
input(type="imstats" interval="60" facility="local0")
# 监控日志
tail -f /var/log/rsyslog-stats.log1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
10.2.2 性能调优
bash
# 高性能配置建议
global(workDirectory="/var/lib/rsyslog")
global(maxMessageSize="64k")
module(load="imudp")
input(type="imudp"
port="514"
threads="4"
RateLimit.interval="30"
RateLimit.burst="20000")
action(type="omfile"
file="/var/log/app.log"
asyncWriting="on"
rotation.sizeLimit="100m")1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
10.3 故障排查
10.3.1 常见问题
| 问题 | 可能原因 | 解决方案 |
|---|---|---|
| 服务无法启动 | 配置语法错误 | 使用 rsyslogd -N1 检查语法 |
| 日志不写入 | 权限问题 | 检查文件和目录权限 |
| 性能问题 | 队列积压 | 增加工作线程和队列大小 |
| 网络连接失败 | 防火墙或端口问题 | 检查防火墙规则和端口监听 |
10.3.2 调试技巧
bash
# 启用调试模式
rsyslogd -dn
# 查看网络连接
netstat -tlnp | grep :514
# 测试日志发送
logger -n localhost -p local0.info "Test message"
# 查看队列状态
rsyslogd -T # 显示线程和队列信息1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
生产环境注意事项
- 定期备份配置:配置文件纳入版本控制
- 监控磁盘空间:日志文件可能快速增长
- 安全加固:限制网络访问,只允许信任主机
- 性能监控:建立性能基线,监控关键指标